package club.looli.haoke.houseResources.service.impl;

import club.looli.haoke.common.vo.PageInfo;
import club.looli.haoke.houseResources.entity.HouseResources;
import club.looli.haoke.houseResources.mapper.HouseResourcesMapper;
import club.looli.haoke.houseResources.service.HouseResourcesService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * Created by looli on 2020/7/7
 * </p>
 *
 * <p>
 *
 * </p>
 *
 * @author looli
 * @since 2020/7/7
 */
@Service
@Transactional
public class HouseResourcesServiceImpl extends ServiceImpl<HouseResourcesMapper,HouseResources> implements HouseResourcesService {

    @Autowired
    private BaseMapper<HouseResources> baseMapper;

    @Override
    public int saveHouseResources(HouseResources houseResources) {
        // 编写校验逻辑，如果校验不通过，返回-1
        if (StringUtils.isBlank(houseResources.getTitle())) {
            return -1;
        }
        // 其他校验以及逻辑省略 ……
        return baseMapper.insert(houseResources);
    }

    @Override
    public PageInfo<HouseResources> queryHouseResourcesList(int page, int pageSize, HouseResources queryCondition) {
        //封装分页信息
        Page<HouseResources> houseResourcesPage = new Page<>(page, pageSize);

        //查询条件
        QueryWrapper<HouseResources> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("updated");

        Page<HouseResources> selectPage = baseMapper.selectPage(houseResourcesPage, wrapper);


        //结果封装
        return new  PageInfo<HouseResources> (selectPage.getTotal(), selectPage.getCurrent(),selectPage.getSize(), selectPage.getRecords());
    }

    @Override
    public HouseResources queryHouseResourcesById(Long id) {
        return baseMapper.selectById(id);
    }
}
